package xtgl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import com.google.gson.Gson;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Page;

public class RoleController extends Controller {
	
	public static int pagen=10;
	public static String selejsmc="";
	public static String selejsms="";
	public static String selejszt="";
	

	// 跳转jsgl.html
	public void index() {
		pagen=10;
		selejsmc="";
		selejsms="";
		selejszt="";
		List<Department> deptlist = Department.dao.find("select * from dept");			//查询所有的部门放在deptlist中
		setAttr("rp", Role_Privilege.dao.find("select * from role_privilege"));
		setAttr("dept", deptlist);
		setAttr("list", Role.dao.getRoles());
		setAttr("privilege", Privilege.dao.getPrivilege());

		List<Map<String, Object>> deptmapList = new ArrayList<Map<String, Object>>();
		for (int i = 0; i < deptlist.size(); i++) {
			Map<String, Object> deptmap = new LinkedHashMap<String, Object>();
			deptmap.put("id", deptlist.get(i).getInt("dept_id"));
			deptmap.put("pid", deptlist.get(i).getInt("prent_id"));
			deptmap.put("name", deptlist.get(i).get("dept_name"));
			if (deptlist.get(i).getInt("prent_id").equals(0))
				deptmap.put("isParent", true);
			deptmap.put("click",
					"democlick(" + deptlist.get(i).getInt("dept_id") + ")");
			deptmapList.add(deptmap);
		}

		Gson gson = new Gson();
		String strList = gson.toJson(deptmapList);
		strList = strList.replaceAll("\"id\"", "id");
		strList = strList.replaceAll("\"pid\"", "pId");
		strList = strList.replaceAll("\"name\"", "name");
		strList = strList.replaceAll("\"click\"", "click");
		setAttr("strList", strList);
		renderFreeMarker("/xtgl/jsgl.html");
	}

	// ͨ删除角色
	public void delerolebyid() {
		Role_Privilege.dao.deleteDeptPrivilegeByRoleId(getParaToInt("role_id")); // 根据roleId删除角色的权限
		Dept_Role.dao.deleteDeptRole(getParaToInt("role_id")); // 根据roleId在部门中删除角色
		Role.dao.deleteRole(getParaToInt("role_id")); // 根据roleId删除角色
		renderText("success");
	}

	// 角色查询
	public void sele() {
		selejsmc=getPara("selejsmc");
		selejsms=getPara("selejsms");
		selejszt=getPara("selejszt");
		renderNull();
		
	}

	// 添加角色
	public void addrole() {
		Role role = getModel(Role.class, "role");
		role.save();
		Dept_Role dept_role = new Dept_Role();
		dept_role.set("dept_id", role.getInt("dept_id"));
		dept_role.set("role_id", role.getInt("role_id"));
		dept_role.save();
		Integer roleId = role.getInt("role_id");
		String j[]=getPara("privilege_id").split(",");		
		for(int i=0;i<j.length;i++){
			Role_Privilege rp=new Role_Privilege();
			rp.set("role_id", roleId).set("privilege_id", Integer.valueOf(j[i])).save();
		}
		
		redirect("/#role");

	}

	public void toUpdaterole() {
		renderJson(Role.dao.findById(getParaToInt("role_id")));// 获取前端传递的id
	}

	// 更改角色
	public void update() {
		Role role = getModel(Role.class);
		role.update();
		renderText("success");
	}

	// 更该角色权限
	public void upprivilege() {

		Integer roleId = getParaToInt("role.role_id");
		String j[]=getPara("privilege_id").split(",");
		Integer[] check = new Integer[j.length];
		for(int i=0;i<j.length;i++){
			check[i]=Integer.valueOf(j[i]);
		}
		Role_Privilege.dao.updateDeptPrivilege(roleId, check);
		redirect("/#role");

	}

	// 分页
	public void pagedemo() {
		List<Object> listrole=new ArrayList<Object>();
		Map<String , Object> map=new HashMap<String, Object>();
		List<Department> depList  =Department.dao.find("select * from dept");
		int n = getParaToInt("pageNumber", 1);
		
		 if(selejsmc!=""&&selejsms==""&&selejszt==""){
			Page<Role> list = Role.dao.paginate(n, pagen, "select *",
					"from role where role_name like '%"
							+ selejsmc+ "%' ");
			map.put("deptlist", depList);
			map.put("list", list);
			listrole.add(map);
			renderJson(listrole);
		}else if (selejsmc==""&&selejsms!=""&&selejszt=="") {
			Page<Role> list = Role.dao.paginate(n, pagen, "select *",
					"from role where role_des like '%"
							+ selejsms+ "%' ");
			map.put("deptlist", depList);
			map.put("list", list);
			listrole.add(map);
			renderJson(listrole);
		}else if (selejsmc==""&&selejsms==""&&selejszt!="") {
			if (selejszt.equals("启用")) {
				
				Page<Role> list = Role.dao.paginate(n, pagen, "select *",
						"from role where status=true");
				map.put("deptlist", depList);
				map.put("list", list);
				listrole.add(map);
				renderJson(listrole);
			} else if (selejszt.equals("停用")) {
				Page<Role> list = Role.dao.paginate(n, pagen, "select *",
						"from role where status=false");
				map.put("deptlist", depList);
				map.put("list", list);
				listrole.add(map);
				renderJson(listrole);
			}
		}else {
			Page<Role> list = Role.dao.paginate(n, pagen, "select *", "from role");
			map.put("deptlist", depList);
			map.put("list", list);
			listrole.add(map);
			renderJson(listrole);
		}
	}

	// 查询部门下的权限id
	public void pid() {
		int dept_id = getParaToInt("dep_id");
		List<Dept_Privilege> demo = Dept_Privilege.dao
				.find("select * from dept_privilege where dept_id=" + dept_id);
		List<Privilege> pList = new ArrayList<Privilege>();
		List<Map<String, Object>> primapList = new ArrayList<Map<String, Object>>();				//创建primaplist用来放权限与list信息
		for (int i = 0; i < demo.size(); i++) {
			pList.add(Privilege.dao.findById(demo.get(i).get("privilege_id")));
		}
		for(int i=0;i<pList.size();i++){
			Map<String, Object> primap = new LinkedHashMap<String, Object>();						//创建map用来放权限信息			
			primap.put("id", pList.get(i).get("privilege_id"));
			primap.put("pId", pList.get(i).get("parent_id"));
			primap.put("name", pList.get(i).get("privilege_name"));
			System.out.println(pList.get(i).get("parent_id"));
			if (pList.get(i).get("parent_id").equals(0))
				primap.put("isParent", true);
			primapList.add(primap);
		}
		Gson ggson = new Gson();
		String strrList = ggson.toJson(primapList);
		strrList = strrList.replaceAll("\"id\"", "id");
		strrList = strrList.replaceAll("\"pId\"", "pId");
		strrList = strrList.replaceAll("\"name\"", "name");
//		renderJson(strrList);
		renderText(strrList);
	}
	// 查询部门下的权限id
	public void pidd() {
		List<Role> roles=Role.dao.find("select * from role where role_id='"+getParaToInt("role_id")+"'");		//根据role_id查询到该角色
//		int dept_id = getParaToInt("dep_id");
		List<Role_Privilege> role_Pri=Role_Privilege.dao.find("select * from role_privilege where role_id= '"+roles.get(0).getInt("role_id")+"'");//根据role_id查询到角色下的权限
		List<Dept_Privilege> demo = Dept_Privilege.dao
				.find("select * from dept_privilege where dept_id=" + roles.get(0).getInt("dept_id"));			//根据角色的dept_id查询角色所属部门下的权限
		List<Privilege> pList = new ArrayList<Privilege>();
		List<Map<String, Object>> primapList = new ArrayList<Map<String, Object>>();				//创建primaplist用来放权限与list信息
		for (int i = 0; i < demo.size(); i++) {
			pList.add(Privilege.dao.findById(demo.get(i).get("privilege_id")));
		}
		
		for(int i=0;i<pList.size();i++){
			System.out.println(pList.get(i).get("privilege_id")+"!!!!!!!!!!!!!"+i);
			Map<String, Object> primap = new LinkedHashMap<String, Object>();						//创建map用来放权限信息
			primap.put("id", pList.get(i).get("privilege_id"));
			primap.put("pId", pList.get(i).get("parent_id"));
			primap.put("name", pList.get(i).get("privilege_name"));
			for(int j=i;j<role_Pri.size();j++){
				if (roles.get(0).getInt("role_id").equals(role_Pri.get(j).getInt("role_id"))&&primap.get("checked")==null) {
						primap.put("checked", "true");
				}
			}
			System.out.println(primap.get("checked"));
			System.out.println(pList.get(i).get("privilege_name"));
			if (pList.get(i).get("parent_id").equals(0))
				primap.put("isParent", true);
			primapList.add(primap);
		}
		Gson ggson = new Gson();
		String strrList = ggson.toJson(primapList);
		strrList = strrList.replaceAll("\"id\"", "id");
		strrList = strrList.replaceAll("\"pId\"", "pId");
		strrList = strrList.replaceAll("\"name\"", "name");
		strrList = strrList.replaceAll("\"checked\"", "checked");
//		renderJson(strrList);
		renderText(strrList);
	}
	
	//更改每一页显示的行数
	public void gghs(){
		pagen=getParaToInt("pagen");
		renderText("success");
	}

}
